package com.example.forum.service.impl;

import com.example.forum.dao.DiscussPostDao;
import com.example.forum.dao.UserDao;
import com.example.forum.domain.DiscussPost;
import com.example.forum.domain.User;
import com.example.forum.utils.SimpleUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.TransactionCallback;
import org.springframework.transaction.support.TransactionTemplate;

import java.util.Date;

/*
    事务测试
 */
@Service
public class TestService {
    @Autowired
    private UserDao userDao;
    @Autowired
    private DiscussPostDao discussPostDao;
    @Autowired
    private TransactionTemplate transactionTemplate;

    //开启事务为读读取已提交（声明式事务）
    /*
        REQUIRED：支持当前事务，如果当前事务不存在，则创建新的事务
        REQUIRED_NEW: 创建一个新事物，并且暂停当前事务
        NESTED: 如果存在当前事务，则嵌套在当前事务中执行（独立的回滚和提交），否则和REQUIRED一样
     */
    @Transactional(isolation = Isolation.READ_COMMITTED,propagation = Propagation.REQUIRED)
    public void testTransaction(){
        //增加一个用户
        User user=new User();
        user.setUsername("test");
        String salt=SimpleUtils.generateUuid().substring(0,5);
        user.setPassword("123"+ salt);
        user.setSalt(salt);
        user.setEmail("123@qq.com");
        userDao.register(user);
        //增加一个帖子
        DiscussPost discussPost=new DiscussPost();
        discussPost.setTitle("事务测试");
        discussPost.setContent("哈哈");
        discussPost.setCreateTime(new Date());
        discussPostDao.addDiscussPost(discussPost);
        int i=10/0;
    }

    //编程式事务
    public void testTransaction2(){
        //设置事务的隔离级别
        transactionTemplate.setIsolationLevel(TransactionDefinition.ISOLATION_READ_COMMITTED);
        //设置事务的传播机制
        transactionTemplate.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
        //执行的方法
        transactionTemplate.execute(new TransactionCallback<Object>() {
            @Override
            public Object doInTransaction(TransactionStatus transactionStatus) {
                //增加一个用户
                User user=new User();
                user.setUsername("test");
                String salt=SimpleUtils.generateUuid().substring(0,5);
                user.setPassword("123"+ salt);
                user.setSalt(salt);
                user.setEmail("123@qq.com");
                userDao.register(user);
                //增加一个帖子
                DiscussPost discussPost=new DiscussPost();
                discussPost.setTitle("事务测试");
                discussPost.setContent("哈哈");
                discussPost.setCreateTime(new Date());
                discussPostDao.addDiscussPost(discussPost);
                int i=10/0;
                return null;
            }
        });
    }

}
